using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using AuthService.Domain.Auth;

namespace AuthService.Application.Auth;

/// <summary>
/// 认证会话仓储接口
/// </summary>
public interface IAuthSessionRepository : IRepository<AuthSession, Guid>
{
    /// <summary>
    /// 根据用户ID获取活跃会话
    /// </summary>
    Task<List<AuthSession>> GetActiveSessionsByUserIdAsync(
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据会话令牌获取会话
    /// </summary>
    Task<AuthSession> GetBySessionTokenAsync(
        string sessionToken,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据访问令牌ID获取会话
    /// </summary>
    Task<AuthSession> GetByAccessTokenIdAsync(
        string accessTokenId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据设备ID获取会话
    /// </summary>
    Task<List<AuthSession>> GetByDeviceIdAsync(
        string deviceId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取过期的会话
    /// </summary>
    Task<List<AuthSession>> GetExpiredSessionsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 撤销用户的所有会话
    /// </summary>
    Task RevokeAllSessionsByUserIdAsync(
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 撤销指定设备的会话
    /// </summary>
    Task RevokeSessionsByDeviceIdAsync(
        string deviceId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 清理过期会话
    /// </summary>
    Task<int> CleanupExpiredSessionsAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取用户的会话统计
    /// </summary>
    Task<AuthSessionStatisticsDto> GetSessionStatisticsAsync(
        Guid userId,
        CancellationToken cancellationToken = default);
}

/// <summary>
/// 认证会话统计DTO
/// </summary>
public class AuthSessionStatisticsDto
{
    /// <summary>
    /// 总会话数
    /// </summary>
    public int TotalSessions { get; set; }

    /// <summary>
    /// 活跃会话数
    /// </summary>
    public int ActiveSessions { get; set; }

    /// <summary>
    /// 已过期会话数
    /// </summary>
    public int ExpiredSessions { get; set; }

    /// <summary>
    /// 已撤销会话数
    /// </summary>
    public int RevokedSessions { get; set; }

    /// <summary>
    /// 最后登录时间
    /// </summary>
    public DateTime? LastLoginTime { get; set; }

    /// <summary>
    /// 设备数量
    /// </summary>
    public int DeviceCount { get; set; }
}